package com.djzhou.gmms.email.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import jodd.util.StringUtil;

import com.djzhou.gmms.base.action.BaseAction;
import com.djzhou.gmms.model.announcement.TbAnnouncement;
import com.djzhou.gmms.model.email.TbAttachment;
import com.djzhou.gmms.model.email.TbEmail;
import com.djzhou.gmms.model.message.TbMessage;

public class EmailAction extends BaseAction<TbEmail>{
	private TbEmail email;
	private List<TbEmail> list;
	private Integer emailId;
	private Integer todaySum;//今日邮件数量
	private Integer unreadSum;//未读数量
	
	private String receiverIdStr;//邮件接收人ID串
	
	private String attachmentName;
	private String attachmentPath;
	private List<TbAttachment> attachmentList;
	
	
	/**
	 * 查询收件箱
	 * @return
	 */
	public String queryReceiver(){
		if(loadData==null)return SUCCESS;
		pageBean=emailService.queryReceiver(queryUser().getUserId(), pageSize, currentPage);
		list=pageBean.getList();
		if(currentPage>1){
			if(list==null || list.size()==0){
				setCurrentPage(getCurrentPage()-1);
				pageBean = emailService.queryReceiver(queryUser().getUserId(), pageSize, currentPage);
				list=pageBean.getList();
			}
		}
		if(list!=null){
			totalCount=pageBean.getAllRow();
		}else{
			totalCount = 0;
			list = new ArrayList<TbEmail>();
		}
		totalPage=pageBean.getTotalPage();
		todaySum=emailService.queryTodaySum(queryUser().getUserId());
		unreadSum=emailService.queryUnreadSum(queryUser().getUserId());
		return SUCCESS;
	}
	/**
	 * 查询发件箱
	 * @return
	 */
	public String querySender(){
		if(loadData==null)return SUCCESS;
		pageBean=emailService.querySender(queryUser().getUserId(), pageSize, currentPage);
		list=pageBean.getList();
		if(currentPage>1){
			if(list==null || list.size()==0){
				setCurrentPage(getCurrentPage()-1);
				pageBean = emailService.querySender(queryUser().getUserId(), pageSize, currentPage);
				list=pageBean.getList();
			}
		}
		if(list!=null){
			totalCount=pageBean.getAllRow();
		}else{
			totalCount = 0;
			list = new ArrayList<TbEmail>();
		}
		totalPage=pageBean.getTotalPage();
		todaySum=emailService.queryTodaySum(queryUser().getUserId());
		unreadSum=emailService.queryUnreadSum(queryUser().getUserId());
		return SUCCESS;
	}
	/**
	 * 查询星级邮件
	 * @return
	 */
	public String queryStar(){
		if(loadData==null)return SUCCESS;
		pageBean=emailService.queryStar(queryUser().getUserId(), pageSize, currentPage);
		list=pageBean.getList();
		if(currentPage>1){
			if(list==null || list.size()==0){
				setCurrentPage(getCurrentPage()-1);
				pageBean = emailService.queryStar(queryUser().getUserId(), pageSize, currentPage);
				list=pageBean.getList();
			}
		}
		if(list!=null){
			totalCount=pageBean.getAllRow();
		}else{
			totalCount = 0;
			list = new ArrayList<TbEmail>();
		}
		totalPage=pageBean.getTotalPage();
		todaySum=emailService.queryTodaySum(queryUser().getUserId());
		unreadSum=emailService.queryUnreadSum(queryUser().getUserId());
		return SUCCESS;
	}
	/**
	 * 查询已删除
	 * @return
	 */
	public String queryDelete(){
		if(loadData==null)return SUCCESS;
		pageBean=emailService.queryDelete(queryUser().getUserId(), pageSize, currentPage);
		list=pageBean.getList();
		if(currentPage>1){
			if(list==null || list.size()==0){
				setCurrentPage(getCurrentPage()-1);
				pageBean = emailService.queryDelete(queryUser().getUserId(), pageSize, currentPage);
				list=pageBean.getList();
			}
		}
		if(list!=null){
			totalCount=pageBean.getAllRow();
		}else{
			totalCount = 0;
			list = new ArrayList<TbEmail>();
		}
		totalPage=pageBean.getTotalPage();
		todaySum=emailService.queryTodaySum(queryUser().getUserId());
		unreadSum=emailService.queryUnreadSum(queryUser().getUserId());
		return SUCCESS;
	}
	/**
	 * 查询草稿箱
	 * @return
	 */
	public String queryDraft(){
		if(loadData==null)return SUCCESS;
		pageBean=emailService.queryDraft(queryUser().getUserId(), pageSize, currentPage);
		list=pageBean.getList();
		if(currentPage>1){
			if(list==null || list.size()==0){
				setCurrentPage(getCurrentPage()-1);
				pageBean = emailService.queryDraft(queryUser().getUserId(), pageSize, currentPage);
				list=pageBean.getList();
			}
		}
		if(list!=null){
			totalCount=pageBean.getAllRow();
		}else{
			totalCount = 0;
			list = new ArrayList<TbEmail>();
		}
		totalPage=pageBean.getTotalPage();
		todaySum=emailService.queryTodaySum(queryUser().getUserId());
		unreadSum=emailService.queryUnreadSum(queryUser().getUserId());
		return SUCCESS;
	}
	
	
	@Override
	public String delete() {
		emailService.delete(emailId);
		msg="删除成功";
		setCurrentPage(currentPage);
		logService.addLog("内部邮件",queryUser(),"删除",emailId,"TbEmail",super.queryCompany(),super.queryDepartment());
		return SUCCESS;
	}
	
	public String deleteReceiver(){
		emailService.deleteReceiver(emailId);
		logService.addLog("内部邮件",queryUser(),"删除收到的邮件",emailId,"TbEmail",super.queryCompany(),super.queryDepartment());
		return SUCCESS;
	}
	public String deleteSender(){
		emailService.deleteSender(emailId);
		logService.addLog("内部邮件",queryUser(),"删除发送的邮件",emailId,"TbEmail",super.queryCompany(),super.queryDepartment());
		return SUCCESS;
	}
	public String deleteDraft(){
		emailService.deleteDraft(emailId);
		logService.addLog("内部邮件",queryUser(),"删除邮件草稿",emailId,"TbEmail",super.queryCompany(),super.queryDepartment());
		return SUCCESS;
	}
	public String deleteDelete(){
		emailService.deleteDelete(emailId);
		logService.addLog("内部邮件",queryUser(),"删除邮件草稿",emailId,"TbEmail",super.queryCompany(),super.queryDepartment());
		return SUCCESS;
	}

	@Override
	public String modify() {
		emailService.update(email);
		msg="修改成功";
		setCurrentPage(currentPage);
		logService.addLog("内部邮件",queryUser(),"编辑",email.getEmailId(),"TbEmail",super.queryCompany(),super.queryDepartment());
		return SUCCESS;
	}

	@Override
	public String queryList() {
		HashMap<String, Object> hashMap=new HashMap<String, Object>();
		pageBean=emailService.queryPageBean(hashMap, pageSize, currentPage);
		list=pageBean.getList();
		if(currentPage>1){
			if(list==null || list.size()==0){
				setCurrentPage(getCurrentPage()-1);
				pageBean = emailService.queryPageBean(hashMap, pageSize, currentPage);
				list=pageBean.getList();
			}
		}
		if(list!=null){
			totalCount=pageBean.getAllRow();
		}else{
			totalCount = 0;
			list = new ArrayList<TbEmail>();
		}
		totalPage=pageBean.getTotalPage();
		return SUCCESS;
	}

	@Override
	public String save() {
		if(email!=null){
			String[] array=receiverIdStr.split(",");
			for(String str:array){
				email.setSender(queryUser());
				email.setReceiver(userService.queryById(Integer.parseInt(str)));
				email.setEmailLevel(0);
				email.setSendDate(new Date());
				email.setEmailState(0);
				email.setSendDelete(0);
				email.setReceiveDelete(0);
				emailService.add(email);
				if(StringUtil.isNotEmpty(attachmentName) && StringUtil.isNotEmpty(attachmentPath)){
					String[] nameArray=attachmentName.split(",");
					String[] pathArray=attachmentPath.split(",");
					
					for(int i=0;i<nameArray.length;i++){
						TbAttachment attachment=new TbAttachment();
						attachment.setAttachmentName(nameArray[i]);
						attachment.setAttachmentPath(pathArray[i]);
						attachment.setEmail(email);
						attachment.setAttachmentType(0);
						attachmentService.add(attachment);
					}
				}
				
			}
		}
		setCurrentPage(currentPage);
		logService.addLog("内部邮件",queryUser(),"添加",email.getEmailId(),"TbEmail",queryCompany(),super.queryDepartment());
		return SUCCESS;
	}
	
	public String toStar(){
		email=emailService.queryById(emailId);
		email.setEmailLevel(1);
		emailService.update(email);
		return SUCCESS;
	}
	public String unStar(){
		email=emailService.queryById(emailId);
		email.setEmailLevel(0);
		emailService.update(email);
		return SUCCESS;
	}

	@Override
	public String toAdd() {
		return SUCCESS;
	}

	@Override
	public String toEdit() {
		setCurrentPage(currentPage);
		email=emailService.queryById(emailId);
		attachmentList=attachmentService.queryListByEmail(emailId);
		return SUCCESS;
	}

	public TbEmail getEmail() {
		return email;
	}

	public void setEmail(TbEmail email) {
		this.email = email;
	}

	public List<TbEmail> getList() {
		return list;
	}

	public void setList(List<TbEmail> list) {
		this.list = list;
	}

	public Integer getEmailId() {
		return emailId;
	}

	public void setEmailId(Integer emailId) {
		this.emailId = emailId;
	}
	public Integer getTodaySum() {
		return todaySum;
	}
	public void setTodaySum(Integer todaySum) {
		this.todaySum = todaySum;
	}
	public Integer getUnreadSum() {
		return unreadSum;
	}
	public void setUnreadSum(Integer unreadSum) {
		this.unreadSum = unreadSum;
	}
	public String getReceiverIdStr() {
		return receiverIdStr;
	}
	public void setReceiverIdStr(String receiverIdStr) {
		this.receiverIdStr = receiverIdStr;
	}
	public String getAttachmentName() {
		return attachmentName;
	}
	public void setAttachmentName(String attachmentName) {
		this.attachmentName = attachmentName;
	}
	public String getAttachmentPath() {
		return attachmentPath;
	}
	public void setAttachmentPath(String attachmentPath) {
		this.attachmentPath = attachmentPath;
	}
	public List<TbAttachment> getAttachmentList() {
		return attachmentList;
	}
	public void setAttachmentList(List<TbAttachment> attachmentList) {
		this.attachmentList = attachmentList;
	}
	
	
}
